package jou.cs.service.impl;

import jou.cs.configurator.CONST;
import jou.cs.entity.TComment;
import jou.cs.entity.TUser;
import jou.cs.mapper.ComtRepository;
import jou.cs.mapper.UserRepository;
import jou.cs.service.CommentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentImpl implements CommentService {
    @Resource
    private ComtRepository comtRepository;
    @Resource
    private UserRepository userRepository;

    @Override
    public void addComment(TComment comment) {
        comtRepository.save(comment);
    }

    @Override
    public void deleteComment(Long id) {
        comtRepository.deleteById(id);
    }

    @Override
    public void updateComment(TComment comment) {
        comtRepository.save(comment);
    }

    @Override
    public TComment getCommentById(Long id) {
        return comtRepository.findById(id).orElse(null);
    }

    @Override
    public List<Map<Object, Object>> getCommentByPage(int page) {
        return comtRepository.findAllByTid(CONST.POST, PageRequest.of(page, CONST.PAGE_SIZE))
                .stream().collect(ArrayList::new, (list, comt) -> {
                    HashMap<Object, Object> map = new HashMap<>();
                    map.put("comt", comt);
                    map.put("user", userRepository.findById(comt.getUid()).orElse(null));
                    list.add(map);
                }, ArrayList::addAll);
    }

    @Override
    public List<Map<Object,Object>> getCommentByUserId(Long uid) {
        TUser user = userRepository.findById(uid).orElse(null);
        return comtRepository.findAllByUid(uid).stream()
                .collect(ArrayList::new, (list, comt) -> {
                    HashMap<Object, Object> map = new HashMap<>();
                    map.put("comt", comt);
                    map.put("user", user);
                    list.add(map);
                }, ArrayList::addAll);
    }

    @Override
    public Page<TComment> getCommentByUserIdAndPage(Long uid, int page) {
        return comtRepository.findAllByUid(uid, PageRequest.of(page, CONST.PAGE_SIZE));
    }

    @Override
    public List<Map<String, Object>> getComByPid(Long id) {
        return comtRepository.findAllByPidAndTid(id, CONST.COMMENT).stream().collect(ArrayList::new, (list, comt) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("comt", comt);
            map.put("user", userRepository.findById(comt.getUid()));
            list.add(map);
        }, ArrayList::addAll);
    }

    @Override
    public List<Object> getPageByPid(Long id, Integer page) {
        return comtRepository.findAllByPid(id, PageRequest.of(page, CONST.PAGE_SIZE)).getContent()
                .stream().collect(ArrayList::new, (list, comt) -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("comt", comt);
                    map.put("user", userRepository.findById(comt.getUid()));
                    list.add(map);
                }, ArrayList::addAll);
    }

    @Override
    public Long getMaxId() {
        return comtRepository.count();
    }
}
